package handlers

import (
	"encoding/json"
	"log/slog"

	"lsp-server/internal/protocol"
)

const CompletionRequestMethod = "textDocument/completion"

type Completion struct {
	fileURIToContents *map[string][]string
}

func NewCompletion(fileURIToContents *map[string][]string) *Completion {
	return &Completion{fileURIToContents: fileURIToContents}
}

func (c Completion) Call(params json.RawMessage) (any, error) {
	slog.Info("received completion method")
	completionParams, err := c.parseParams(params)
	if err != nil {
		slog.Error("Error to parse completion params")
		return nil, err
	}
	slog.Debug("completion params", slog.Any("params", *completionParams))

	suggestions := []protocol.CompletionItem{
		{
			Label: "some completion",
			Kind:  protocol.ValueCompletion,
		},
	}

	return suggestions, err
}

func (c Completion) parseParams(params json.RawMessage) (*protocol.CompletionParams, error) {
	var completionParams protocol.CompletionParams
	if err := json.Unmarshal(params, &completionParams); err != nil {
		return nil, err
	}

	return &completionParams, nil
}
